package demo.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import demo.common.Result;
import demo.entity.Major;
import demo.mapper.MajorMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@CrossOrigin(maxAge = 3600)
@RestController
@RequestMapping("/major")
public class MajorController {
  @Resource
  MajorMapper majorMapper;

  @PostMapping
  public Result<?> save(@RequestBody Major major){
    majorMapper.insert(major);
    return Result.success();
  }
  @GetMapping
  public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                            @RequestParam(defaultValue = "10") Integer pageSize,
                            @RequestParam(defaultValue = "") String search) {
    LambdaQueryWrapper<Major> wrapper = Wrappers.<Major>lambdaQuery();
    if (StrUtil.isNotBlank(search)) {
      wrapper.like(Major::getMname, search);
    }
    Page<Major> userPage = majorMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
    return Result.success(userPage);
  }

}
